package com.kevin.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/** 
* 系统配置文件参数获取工具 
* 
* @author lvk 2012-4-23 15:21:22 
*/ 

public final class SysParamsToolkit { 
        private static Properties prop; 
    	private static String filedir="/config.properties";
    	private static long filedate=0;

        private SysParamsToolkit() { 
        } 

        static { 
        	reload(); 
        } 

        public static boolean reload() { 
	        boolean flag = true; 
	        prop = new Properties(); 
	        FileInputStream inputStream = null;
	        try { 
	        	inputStream = new FileInputStream(SysParamsToolkit.class.getResource(filedir).getFile());
	            prop.load(inputStream); 
	            flag = false; 
	        } catch (IOException e) { 
	        	if(inputStream != null)
					try {inputStream.close();} catch (IOException e1) {}
	             e.printStackTrace(); 
	        } 
	        File file=new File(SysParamsToolkit.class.getResource(filedir).getFile());
	        filedate = file.lastModified();
	        return flag; 
        } 

        public static Properties getSysProperties() { 
            return prop; 
        } 

        /** 
         * 获取指定的系统属性值 
         * 
         * @param key 指定的属性名称 
         * @return 指定的系统属性值 
         */ 
        public static String getProperty(String key) { 
            return getProperty(key,null); 
        } 

        /** 
         * 获取指定的系统属性值 
         * 
         * @param key                指定的属性名称 
         * @param defaultVal 默认值 
         * @return 指定的系统属性值 
         */ 
        public static String getProperty(String key, String defaultVal) {
        	File file = new File(SysParamsToolkit.class.getResource(filedir).getFile());		
    		if(filedate !=file.lastModified()){
    			System.out.println("reload==");
    			reload();
    		}
            return prop.getProperty(key, defaultVal); 
        } 

        public static void main(String[] args) { 
//            System.out.println(getProperty("Java6WS.wsip"));
        } 
}